﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Web.Services;
using System.Diagnostics;

public partial class CandidateStatus : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetStatusDashboardDDL();
                //gvStatusDashboard.Columns[0].Visible = false;
                //gvStatusDashboard.Columns[10].Visible = false;
                btnApproved.Visible = false;
                btnRejected.Visible = false;
                if (Session["StatusID"] != null) //add
                {
                    string strUserRole = CommonUtility.GetCurrentUserRole();
                    int intStatusID = Convert.ToInt32(Session["StatusID"]);
                    ddlCandidateStatus.SelectedValue = intStatusID.ToString();
                    if (intStatusID != 2 && intStatusID != 26 && intStatusID != 31)
                    {
                        //IntStatusID is 3 so need to give if condition and display gridview as per that


                        int count = gvStatusDashboard.Rows.Count;
                        if (count <= 0 || strUserRole == "RD")
                        {
                            btnApproved.Visible = false;
                            btnRejected.Visible = false;
                        }
                        else
                        {
                            if (intStatusID == 1 || strUserRole == "RD")
                            {
                                btnApproved.Visible = true;
                                btnRejected.Visible = true;
                            }
                            else
                            {
                                btnApproved.Visible = false;
                                btnRejected.Visible = false;
                            }
                            btnApproved.Visible = false;
                            btnRejected.Visible = false;
                        }

                        if (intStatusID == 3)
                        {
                            GetDataAllocatedAssessor(CommonUtility.GetLoggedOnUserId(), intStatusID);
                            SetReviewForRoles(Convert.ToInt32(Session["StatusID"]));
                            return;
                        }
                        else
                        {
                            GetData(CommonUtility.GetLoggedOnUserId(), intStatusID);

                        }

                        Session["UserID"] = CommonUtility.GetLoggedOnUserId();
                    }
                    else
                    {
                        gvAssigningAssessor.Visible = true;
                       
                        GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intStatusID);
                    }
                    SetReviewForRoles(Convert.ToInt32(Session["StatusID"]));
                }
                else
                {

                }


            }
            catch (Exception ex)
            {
                lblErrorMessage.Text = ex.Message;
            }
        }

    }


    private void GetData(Guid UID, int RNO)
    {
        DataSet objDs;
        objDs = new UserMasterBAL().GetStatusDashboardbyStatusId(UID, RNO);
        gvStatusDashboard.DataSource = objDs;
        gvStatusDashboard.DataBind();
        gvStatusDashboard.Columns[9].Visible = false;
        gvStatusDashboard.Columns[14].Visible = false;
        gvStatusDashboard.Columns[15].Visible = false;
        gvStatusDashboard.Columns[16].Visible = false;
        string strUserRole = CommonUtility.GetCurrentUserRole();
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        btnApproved.Visible = false;
        btnRejected.Visible = false;
        //foreach (DataControlField item in gvStatusDashboard.Columns)
        //{
        //    Debug.WriteLine(string.Format("{0}   {1}", gvStatusDashboard.Columns.IndexOf(item), item.HeaderText));
        //}
        if (gvStatusDashboard != null && objDs != null)
        {
            gvStatusDashboard.Columns[0].Visible = false;
            ////todo nilesh add
            //gvStatusDashboard.Columns[7].Visible = true;
            //gvStatusDashboard.Columns[8].Visible = true;
            //gvStatusDashboard.Columns[0].Visible = false;

            if (strUserRole == "RD" && intstatusID == 1)
            {
                int count = gvStatusDashboard.Rows.Count;
                if (count <= 0)
                {
                    btnApproved.Visible = false;
                    btnRejected.Visible = false;
                }
                else
                {
                    gvStatusDashboard.Columns[0].Visible = true;
                    //brijal rollback as discuss with Pradipsir as on 28-07-2017
                    //gvStatusDashboard.Columns[14].Visible = true;
                    //gvStatusDashboard.Columns[16].Visible = true;
                    btnApproved.Visible = true;
                    btnRejected.Visible = true;
                }
            }
            else
            {
                gvStatusDashboard.Columns[7].Visible = true;
            }
            if (intstatusID == 23 || intstatusID == 1)
            {
                gvStatusDashboard.Columns[8].Visible = false;
            }
        }

        //gvStatusDashboard.Columns[0].Visible = true;
    }

    private void GetDataAllocatedAssessor(Guid UID, int RNO)
    {
        DataSet objDs;
        objDs = new UserMasterBAL().GetStatusDashboardbyAllocatedAssessor(UID, RNO);

        gvAllocatedAssessor.DataSource = objDs;
        gvAllocatedAssessor.DataBind();
        string strUserRole = CommonUtility.GetCurrentUserRole();
        btnApproved.Visible = false;
        btnRejected.Visible = false;
        if (gvAllocatedAssessor != null && objDs != null)
        {
            //gvAllocatedAssessor.Columns[6].Visible = true;
            //gvAllocatedAssessor.Columns[7].Visible = true;
            //gvAllocatedAssessor.Columns[8].Visible = true;
            //gvAllocatedAssessor.Columns[9].Visible = true;
            if (strUserRole == "RD" || strUserRole == "GD")
            {
                //gvAllocatedAssessor.Columns[6].Visible = false;
                //gvAllocatedAssessor.Columns[7].Visible = false;
                //gvAllocatedAssessor.Columns[8].Visible = false;
                //gvAllocatedAssessor.Columns[9].Visible = false;
            }
        }
    }

    private void GetDataAssigningAssessor(Guid UID, int RNO)
    {
        DataSet objDs;
        objDs = new UserMasterBAL().GetStatusDashboardbyAssigningAssessor(UID, RNO);
        gvAssigningAssessor.DataSource = objDs;
        gvAssigningAssessor.DataBind();
        string ROLE = CommonUtility.GetCurrentUserRole();
        if (ROLE != "IQKSAD")
        {
            //gvAssigningAssessor.Columns[6].Visible = false;
            //gvAssigningAssessor.Columns[7].Visible = false;
            //gvAssigningAssessor.Columns[8].Visible = false;
            //gvAssigningAssessor.Columns[9].Visible = false;
            //gvAssigningAssessor.Columns[11].Visible = false;
            btnApproved.Visible = false;
            btnRejected.Visible = false;
        }
        if (ROLE == "IQKSMG" || ROLE == "IQKSAD")
        {
            //gvAssigningAssessor.Columns[6].Visible = false;
            //gvAssigningAssessor.Columns[7].Visible = false;
            //gvAssigningAssessor.Columns[8].Visible = false;
            //gvAssigningAssessor.Columns[9].Visible = false;
            //gvAssigningAssessor.Columns[10].Visible = true;
            //gvAssigningAssessor.Columns[11].Visible = true;
            btnApproved.Visible = false;
            btnRejected.Visible = false;
        }
    }
    protected void gvStatusDashboard_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        gvStatusDashboard.PageIndex = e.NewPageIndex;
        GetData(CommonUtility.GetLoggedOnUserId(), intstatusID);
        SetReviewForRoles(intstatusID);
    }

    public string GetReviewUrl(object objCandidateID, object objBriefID, object objCandidateAccType, object objStatusID)
    {
        string strPath = string.Empty;

        if (Convert.ToString(objCandidateAccType) == "CANM")
        {
            strPath = "~/Assessor/CandidateBriefDetailsAuditorm.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&briefid=" + CommonUtility.Encrypt(Convert.ToString(objBriefID), true) + "&statusid=" + CommonUtility.Encrypt(Convert.ToString(objStatusID), true);
        }
        else if (Convert.ToString(objCandidateAccType) == "CANR")
        {
            strPath = "~/Assessor/CandidateBriefDetailsAuditorrl.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&briefid=" + CommonUtility.Encrypt(Convert.ToString(objBriefID), true) + "&statusid=" + CommonUtility.Encrypt(Convert.ToString(objStatusID), true);
        }
        else if (Convert.ToString(objCandidateAccType) == "CANB")
        {
            //strPath = "~/Assessor/CandidateBriefDetailsAuditor.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&briefid=" + CommonUtility.Encrypt(Convert.ToString(objBriefID), true) + "&statusid=" + CommonUtility.Encrypt(Convert.ToString(objStatusID), true);
            strPath = "~/Assessor/CandidateBriefDetailsAuditorrl.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&briefid=" + CommonUtility.Encrypt(Convert.ToString(objBriefID), true) + "&statusid=" + CommonUtility.Encrypt(Convert.ToString(objStatusID), true);
        }

        if (Convert.ToInt64(ddlCandidateStatus.SelectedValue) == -1)
        {
            strPath = strPath + "&CandQuestion=1";
        }

        return strPath;
    }

    public string GetCertificateUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/Candidate/AccreditationCandidateCertificatePDF.aspx?CandidateID=" + Convert.ToString(objCandidateID);

        return strPath;
    }
    public string GetCandidateResultUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/Candidate/AccreditationCandidateResultPDF.aspx?CandidateID=" + Convert.ToString(objCandidateID);

        return strPath;
    }
    public string GetCandidateResultEmailUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/Candidate/AccreditationCandidateResultEmail.aspx?CandidateID=" + Convert.ToString(objCandidateID);

        return strPath;
    }
    public string GetMakePaymentUrl(object objCandidateID, object objUserId)
    {
        string strPath = string.Empty;
        strPath = "~/Common/RegisterAlready.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&UserId=" + CommonUtility.Encrypt(Convert.ToString(objUserId), true) + "&PayFromAdmin=";

        return strPath;
    }
    public string GetViewtUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/home/CandidateDetailsReg.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true);

        return strPath;
    }
    public string GetRejectUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/home/CandidateApprove.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&CandidateStatusID=" + CommonUtility.Encrypt("26",true) + "&CandidateV=0";

        return strPath;
    }
    public string GetApproveUrl(object objCandidateID)
    {
        string strPath = string.Empty;
        strPath = "~/home/CandidateApprove.aspx?CandidateID=" + CommonUtility.Encrypt(Convert.ToString(objCandidateID), true) + "&CandidateStatusID=" + CommonUtility.Encrypt("25",true) + "&CandidateV=0";

        return strPath;
    }

    private void GetStatusDashboardDDL()
    {

        ddlCandidateStatus.DataSource = GetStatusDashboardDataSet();
        ddlCandidateStatus.DataTextField = "StatusCode";
        ddlCandidateStatus.DataValueField = "StatusID";
        ddlCandidateStatus.DataBind();
        ddlCandidateStatus.Items.Insert(0, new ListItem("-- Select Candidate Status --", "0"));
        //int intStatusID = Convert.ToInt32(Session["StatusID"]);

        //ddlCandidateStatus.Items.Insert(intStatusID,"");
    }

    private DataSet GetAssessorByRole(string strRoleName, int Candidateid)
    {
        DataSet objDs;
        objDs = new UserMasterBAL().GetAssessorByRole(strRoleName, Candidateid);
        if (objDs.Tables.Count > 1)
        {
            ViewState["AssessorFees"] = objDs.Tables[1];
        }
        return objDs;
    }

    private DataSet GetStatusDashboardDataSet()
    {

        DataSet objDs = null;
        try
        {
            objDs = new UserMasterBAL().GetStatusDashboard(CommonUtility.GetLoggedOnUserId());
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    protected void ddlCandidateStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        //TODO
        //Guid UserId = new Guid("8B551132-48EC-43DE-9C9E-1D389A8EBA99");
        lblErrorMessage.Text = "";
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        gvAssigningAssessor.Visible = false;
        gvAllocatedAssessor.Visible = false;
        gvStatusDashboard.Visible = false;
        string strUserRole = CommonUtility.GetCurrentUserRole();

        if (intstatusID == 2 || intstatusID == 26 || intstatusID == 31)
        {
            gvAssigningAssessor.Visible = true;
            GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
        }
        else if (intstatusID == 3)
        {
            gvAllocatedAssessor.Visible = true;
            GetDataAllocatedAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
        }
        else if (intstatusID == 0)
        {
            Response.Redirect("~/home/CandidateAccrediationDetails.aspx");
        }
        else
        {
            gvStatusDashboard.Visible = true;

            GetData(CommonUtility.GetLoggedOnUserId(), intstatusID);
        }
        SetReviewForRoles(intstatusID);
    }

    protected void ddlAssessor_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dtAsFees = (DataTable)ViewState["AssessorFees"];

        DropDownList lb = (DropDownList)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        TextBox txtAsFees = (TextBox)gvRow.FindControl("txtAssessorFees");
        if (lb.SelectedValue != "0")
        {
            txtAsFees.Text = dtAsFees.Select("AssessorID=" + lb.SelectedValue.ToString())[0].ItemArray[1].ToString();
        }
        else
        {
            txtAsFees.Text = "";
        }
    }
    protected void ddlResearchLead_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dtRLFees = (DataTable)ViewState["AssessorFees"];
        DropDownList lb = (DropDownList)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        TextBox txtRLFees = (TextBox)gvRow.FindControl("txtResearchLeadFees");
        if (lb.SelectedValue != "0")
        {
            txtRLFees.Text = dtRLFees.Select("AssessorID=" + lb.SelectedValue.ToString())[0].ItemArray[1].ToString();
        }
        else
        {
            txtRLFees.Text = "";
        }
    }
    protected void ddlAuditor_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dtAdtFees = (DataTable)ViewState["AssessorFees"];
        DropDownList lb = (DropDownList)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        TextBox txtAdFees = (TextBox)gvRow.FindControl("txtAuditorFees");
        if (lb.SelectedValue != "0")
        {
            txtAdFees.Text = dtAdtFees.Select("AssessorID=" + lb.SelectedValue.ToString())[0].ItemArray[1].ToString();
        }
        else
        {
            txtAdFees.Text = "";
        }
    }
    protected void ddlExpert_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dtExFees = (DataTable)ViewState["AssessorFees"];
        DropDownList lb = (DropDownList)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        TextBox txtExFees = (TextBox)gvRow.FindControl("txtExpertFees");
        if (lb.SelectedValue != "0")
        {
            txtExFees.Text = dtExFees.Select("AssessorID=" + lb.SelectedValue.ToString())[0].ItemArray[1].ToString();
        }
        else
        {
            txtExFees.Text = "";
        }
    }

    protected void gvAssigningAssessor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            //LinkButton lnkAllocateAssessor = (LinkButton)e.Row.FindControl("btnPopUp");
            Label lblCandidateType = (Label)e.Row.FindControl("lblCandidateCode");

            //Label lblPartnerID = (Label)e.Row.FindControl("lblPartnerID");

            //if (Convert.ToString(objCandidateAccType) == "CANM")
            
            //if ((Convert.ToString(lblCandidateType.Text) == "CANM" || Convert.ToString(lblCandidateType.Text) == "CANR"))
            //{
            //    if(Convert.ToInt16(ddlCandidateStatus.SelectedValue.Trim()) == 2)
            //    {
            //        lnkAllocateAssessor.Text = "";
            //    }
            //}


            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
              
                TextBox txtRLFees = (TextBox)e.Row.FindControl("txtResearchLeadFees");
                TextBox txtAFees = (TextBox)e.Row.FindControl("txtAssessorFees");

                RequiredFieldValidator rqA = (RequiredFieldValidator)e.Row.FindControl("reqddlAssessor");
                RequiredFieldValidator rqAFees = (RequiredFieldValidator)e.Row.FindControl("reqAssessorFees");
                RequiredFieldValidator rqRL = (RequiredFieldValidator)e.Row.FindControl("reqddlResearchLead");
                RequiredFieldValidator rqRLFees = (RequiredFieldValidator)e.Row.FindControl("reqResearchLeadFees");

                DropDownList dp = (DropDownList)e.Row.FindControl("ddlAssessor");
                Label CandidateID = (Label)e.Row.FindControl("lblCandidateID");


                int Candidateid = 0;
                if (CandidateID != null)
                { Candidateid = Convert.ToInt32(CandidateID.Text); }

                if (dp != null)
                {
                    //Label lbCandidateCode = e.Row["lblCandidateCode"].Text;
                    //Label lbCandidateCode = (Label)gvAssigningAssessor.Rows[0].FindControl("lblCandidateCode");
                    //string strCandidateCode = Convert.ToString(lbCandidateCode.Text);    
                    //e.Row.Cells["lblCandidateCode"].Text;

                    dp.DataSource = GetAssessorByRole("RAM", Candidateid);
                    dp.DataTextField = "AssessorName";
                    dp.DataValueField = "AssessorID";
                    dp.DataBind();
                    dp.Items.Insert(0, new ListItem("-- Allocate Moderator --", "0"));
                }

                DropDownList dprl = (DropDownList)e.Row.FindControl("ddlResearchLead");
                if (dprl != null)
                {
                    dprl.DataSource = GetAssessorByRole("RARL", Candidateid);
                    dprl.DataTextField = "AssessorName";
                    dprl.DataValueField = "AssessorID";
                    dprl.DataBind();
                    dprl.Items.Insert(0, new ListItem("-- Allocate ResearchLead --", "0"));
                }

                if (lblCandidateType != null)
                {
                    if (lblCandidateType.Text == "CANM")
                    {
                        dprl.Enabled = false;
                        txtRLFees.Enabled = false;
                        rqRL.Enabled = false;
                        rqRLFees.Enabled = false;
                        dprl.Visible = false; // to make visible false
                        txtRLFees.Visible = false;
                        //   e.Row.Cells[6].Visible = false;
                        //if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
                        //{
                        //    e.Row.Cells[7].Visible = false;//this is your templatefield column.
                        //    }
                        // gvAssigningAssessor.Columns[6].ItemStyle.Width = "20%"; method works at row bound event

                    }
                    if (lblCandidateType.Text == "CANR")
                    {
                        dp.Enabled = false;
                        txtAFees.Enabled = false;
                        rqA.Enabled = false;
                        rqAFees.Enabled = false;
                    }
                }
                DropDownList dpad = (DropDownList)e.Row.FindControl("ddlAuditor");
                if (dpad != null)
                {
                    dpad.DataSource = GetAssessorByRole("AD", Candidateid);
                    dpad.DataTextField = "AssessorName";
                    dpad.DataValueField = "AssessorID";
                    dpad.DataBind();
                    dpad.Items.Insert(0, new ListItem("-- Allocate Auditor --    ", "0"));
                }
                DropDownList dpep = (DropDownList)e.Row.FindControl("ddlExpert");
                if (dpep != null)
                {
                    dpep.DataSource = GetAssessorByRole("IE", Candidateid);
                    dpep.DataTextField = "AssessorName";
                    dpep.DataValueField = "AssessorID";
                    dpep.DataBind();
                    dpep.Items.Insert(0, new ListItem("-- Allocate Industry Expert --    ", "0"));
                }

            }
        }
    }

    protected void gvAssigningAssessor_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvAssigningAssessor.EditIndex = -1;

        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);

    }
    protected void gvAssigningAssessor_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvAssigningAssessor.EditIndex = e.NewEditIndex;
        //Guid UserId = new Guid("8B551132-48EC-43DE-9C9E-1D389A8EBA99");
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
        lblErrorMessage.Text = "";
    }
    protected void gvAssigningAssessor_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        Label lbUserID = (Label)gvAssigningAssessor.Rows[e.RowIndex].FindControl("lblUserID");
        string strUserID = Convert.ToString(lbUserID.Text);

        //Label lbCandidateType = (Label)gvAssigningAssessor.Rows[e.RowIndex].FindControl("lblCandidateType");
        //string strCandidateType = Convert.ToString(lbCandidateType.Text);    

        Label lbCandidateID = (Label)gvAssigningAssessor.Rows[e.RowIndex].FindControl("lblCandidateID");
        string strCandidateID = Convert.ToString(lbCandidateID.Text);

        DropDownList ddlAS = (DropDownList)gvAssigningAssessor.Rows[e.RowIndex].FindControl("ddlAssessor");
        string strAssessor = ddlAS.SelectedValue.Trim();
        string strAssessori = ddlAS.SelectedItem.ToString();
        TextBox txtASF = (TextBox)gvAssigningAssessor.Rows[e.RowIndex].FindControl("txtAssessorFees");
        string strASF = Convert.ToString(txtASF.Text);

        DropDownList ddlRL = (DropDownList)gvAssigningAssessor.Rows[e.RowIndex].FindControl("ddlResearchLead");
        string strResearchLead = ddlRL.SelectedValue.Trim();
        string strResearchLeadi = ddlRL.SelectedItem.ToString();
        TextBox txtRLF = (TextBox)gvAssigningAssessor.Rows[e.RowIndex].FindControl("txtResearchLeadFees");
        string strRLF = Convert.ToString(txtRLF.Text);

        DropDownList ddlAD = (DropDownList)gvAssigningAssessor.Rows[e.RowIndex].FindControl("ddlAuditor");
        string strAuditor = ddlAD.SelectedValue.Trim();
        string strAuditori = ddlAD.SelectedItem.ToString();
        TextBox txtADF = (TextBox)gvAssigningAssessor.Rows[e.RowIndex].FindControl("txtAuditorFees");
        string strADF = Convert.ToString(txtADF.Text);

        DropDownList ddlEP = (DropDownList)gvAssigningAssessor.Rows[e.RowIndex].FindControl("ddlExpert");
        string strExpert = ddlEP.SelectedValue.Trim();
        string strExperti = ddlEP.SelectedItem.ToString();
        TextBox txtEPF = (TextBox)gvAssigningAssessor.Rows[e.RowIndex].FindControl("txtExpertFees");
        string strEPF = Convert.ToString(txtEPF.Text);

        string strReturnValue;
        CandidateAllocateAssessorBAL objCABal = new CandidateAllocateAssessorBAL();
        CandidateAllocateAssessorBO objCABO = new CandidateAllocateAssessorBO();

        objCABO.ModeratorID = Convert.ToInt32(strAssessor);
        objCABO.ResearchLeadID = Convert.ToInt32(strResearchLead);
        if (objCABO.ModeratorID == 0)
        {
            objCABO.ModeratorFees = 0.00;
        }
        else
        {
            objCABO.ModeratorFees = (double)Convert.ToDouble(strASF);
        }
        if (objCABO.ResearchLeadID == 0)
        {
            objCABO.ResearchLeadFees = 0.00;
        }
        else
        {
            objCABO.ResearchLeadFees = (double)Convert.ToDouble(strRLF);
        }
        objCABO.AuditorID = Convert.ToInt32(strAuditor);
        objCABO.IndustryExpertID = Convert.ToInt32(strExpert);
        objCABO.AuditorFees = (double)Convert.ToDouble(strADF);
        objCABO.IndustryExpertFees = (double)Convert.ToDouble(strEPF);
        objCABO.CandidateID = Convert.ToInt32(strCandidateID);
        objCABO.UserID = new Guid(strUserID);

        strReturnValue = (string)objCABal.SetCandidateAllocateAssessor(objCABO);
        Label lbRegNo = (Label)gvAssigningAssessor.Rows[e.RowIndex].FindControl("lblRegistrationNo");
        string strRegNo = Convert.ToString(lbRegNo.Text);

        if (strReturnValue == "SUCCESS")
        {
            //lblErrorMessage.Text = "Assesors allocated successfully.";            
        }
        else if (strReturnValue == "EXIST")
        {
            //lblMessage.Text = "Record already exists."; 
        }
        else
        {
            //lblMessage.Text = "Error while saving record."; 
        }
        gvAssigningAssessor.EditIndex = -1;
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
        if (strReturnValue == "SUCCESS")
        {
            lblErrorMessage.Text = "Assesors allocated successfully (Candidate Registration No: " + strRegNo + ")";
            lblErrorMessage.ForeColor = System.Drawing.Color.Green;
            lblErrorMessage.Font.Bold = true;
            lblErrorMessage.Visible = true;
        }
    }
    protected void gvAllocatedAssessor_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        gvAllocatedAssessor.PageIndex = e.NewPageIndex;
        GetDataAllocatedAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);

    }

    protected void btnApproved_Click(object sender, EventArgs e)
    {
        CandidateDetailsBO cbo = new CandidateDetailsBO();
        CandidateDetailsBAL cbal = new CandidateDetailsBAL();
        string strReturnValue;
        string strCandidateIDs = "";
        string strCandidateRegNos = "";
        string strCandidateApprovAccTyp = "";
        int i = 0;
        foreach (GridViewRow row in gvStatusDashboard.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)(row.Cells[0].FindControl("chkApproveSD"));
                if (chkBox.Checked)
                {
                    int CandidateID = Convert.ToInt32(gvStatusDashboard.DataKeys[i].Value.ToString());
                    strCandidateIDs = strCandidateIDs + "," + Convert.ToString(CandidateID);
                    if (strCandidateRegNos == "")
                    {
                        strCandidateRegNos = Convert.ToString(row.Cells[3].Text);
                    }
                    else
                    {
                        strCandidateRegNos = strCandidateRegNos + ", " + Convert.ToString(row.Cells[3].Text);
                    }
                    var ddl = row.FindControl("ddlRDAccName") as DropDownList;
                    if (ddl != null)
                    {
                        if (strCandidateApprovAccTyp == "")
                        {
                            strCandidateApprovAccTyp = Convert.ToString(ddl.Text);
                        }
                        else
                        {
                            strCandidateApprovAccTyp = strCandidateApprovAccTyp + "," + Convert.ToString(ddl.Text);
                        }
                    }
                }
            }
            i++;
        }
        strCandidateIDs = strCandidateIDs.Substring(1, strCandidateIDs.Length - 1);
      

        strReturnValue = (string)cbal.SetStatusIdRegApproveDateForCandidateDetails(strCandidateIDs, CommonUtility.GetLoggedOnUserId(), strCandidateApprovAccTyp);
        if (strReturnValue == "SUCCESS")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Candidate with Registration No:" + strCandidateRegNos + " approved succesfully.');", true);

            //lblErrorMessage.Text ="Candidate with Registration No:"+strCandidateRegNos+ " approved succesfully.";
            //lblErrorMessage.ForeColor = System.Drawing.Color.Green;            
        }
        else if (strReturnValue == "EXIST")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('already exists.');", true);
            //lblErrorMessage.Text = "already exists.";
            //lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
            //lblErrorMessage.Text = "Error while saving record.";
            //lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
        lblErrorMessage.Font.Bold = true;

        gvStatusDashboard.EditIndex = -1;
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        GetData(CommonUtility.GetLoggedOnUserId(), intstatusID);
    }
    protected void gvAssigningAssessor_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        gvAssigningAssessor.PageIndex = e.NewPageIndex;
        GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
    }
    protected void btnRejected_Click(object sender, EventArgs e)
    {
        CandidateDetailsBO cbo = new CandidateDetailsBO();
        CandidateDetailsBAL cbal = new CandidateDetailsBAL();
        string strReturnValue;
        string strCandidateIDs = "";
        string strCandidateRegNos = "";
        int i = 0;
        foreach (GridViewRow row in gvStatusDashboard.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)(row.Cells[0].FindControl("chkApproveSD"));
                if (chkBox.Checked)
                {
                    int CandidateID = Convert.ToInt32(gvStatusDashboard.DataKeys[i].Value.ToString());
                    strCandidateIDs = strCandidateIDs + "," + Convert.ToString(CandidateID);
                    if (strCandidateRegNos == "")
                    {
                        strCandidateRegNos = Convert.ToString(row.Cells[3].Text);
                    }
                    else
                    {
                        strCandidateRegNos = strCandidateRegNos + ", " + Convert.ToString(row.Cells[3].Text);
                    }
                }
            }
            i++;
        }
        strCandidateIDs = strCandidateIDs.Substring(1, strCandidateIDs.Length - 1);
        strReturnValue = (string)cbal.SetStatusIdRegRejectDateForCandidateDetails(strCandidateIDs, CommonUtility.GetLoggedOnUserId());
        if (strReturnValue == "SUCCESS")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Candidate with Registration No:" + strCandidateRegNos + " rejected succesfully.');", true);

            //lblErrorMessage.Text ="Candidate with Registration No:"+strCandidateRegNos+ " approved succesfully.";
            //lblErrorMessage.ForeColor = System.Drawing.Color.Green;
        }
        else if (strReturnValue == "EXIST")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('already exists.');", true);
            //lblErrorMessage.Text = "already exists.";
            //lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
            //lblErrorMessage.Text = "Error while saving record.";
            //lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
        lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        lblErrorMessage.Font.Bold = true;

        gvStatusDashboard.EditIndex = -1;
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        GetData(CommonUtility.GetLoggedOnUserId(), intstatusID);
    }

    protected void SetReviewForRoles(int SID)
    {
        string ROLE = CommonUtility.GetCurrentUserRole();
        if (ROLE == "GD" || ROLE == "RD" || ROLE == "IQKSAD" || ROLE == "IQKSMG")
        {
            if (SID == 18 || SID == 20)
            {
                gvStatusDashboard.Columns[9].Visible = true;
            }
            else
            {
                gvStatusDashboard.Columns[9].Visible = false;
            }

            if (ROLE == "GD" && SID == 18 || SID == 5 || SID == 6 || SID == 7 || SID == 8 || SID == 9 || SID == 24 || SID == 26 || SID == 25)
            {
                HideColumns(true);
            }
            else if (ROLE == "RD" && SID == 16 || SID == 5 || SID == 6 || SID == 7 || SID == 8 || SID == 9 || SID == 24 || SID == 26 || SID == 25)
            {
                HideColumns(true);
            }
            else
            {
                HideColumns(false);
            }

            if (SID == 1 || SID == 23 || SID == 2 || SID == 26 || SID == 25 || SID == 3 || SID == 4 || SID == 22 || SID == 31)
            {
                gvStatusDashboard.Columns[7].Visible = true;
                gvStatusDashboard.Columns[8].Visible = false;
                gvStatusDashboard.Columns[9].Visible = false;
            }
            else
            {
                gvStatusDashboard.Columns[7].Visible = false;
            }


            if (ROLE == "IQKSAD" || ROLE == "IQKSMG")
            {
                gvAllocatedAssessor.Columns[11].Visible = false;
                gvAllocatedAssessor.Columns[13].Visible = false;
                gvAllocatedAssessor.Columns[15].Visible = true;
                gvAllocatedAssessor.Columns[16].Visible = true;
                gvStatusDashboard.Columns[11].Visible = true;
                gvAssigningAssessor.Columns[12].Visible = true;

                if (SID == 25)
                {
                    gvStatusDashboard.Columns[11].Visible = true;
                }
                else
                {
                    gvStatusDashboard.Columns[11].Visible = false;
                }
            }
            else
            {
                gvAllocatedAssessor.Columns[11].Visible = false;
                gvAllocatedAssessor.Columns[13].Visible = false;
                gvStatusDashboard.Columns[11].Visible = false;
                gvAssigningAssessor.Columns[12].Visible = false;
                gvAllocatedAssessor.Columns[15].Visible = false;
                gvAllocatedAssessor.Columns[16].Visible = false;
            }
            if (SID == 18)
            {
                gvStatusDashboard.Columns[10].Visible = true;
                gvStatusDashboard.Columns[12].Visible = true;
                gvStatusDashboard.Columns[13].Visible = true;
            }
            else if (SID == 19 && (ROLE == "IQKSAD" || ROLE == "IQKSMG"))
            { }
            else
            {
                gvStatusDashboard.Columns[10].Visible = false;
                gvStatusDashboard.Columns[12].Visible = false;
                gvStatusDashboard.Columns[13].Visible = false;
            }

            //if (ROLE == "RD")
            //{
            //    gvStatusDashboard.Columns[10].Visible = false;
            //}
            //else
            //{
            //    gvStatusDashboard.Columns[10].Visible = false;
            //}
        }

        if (ROLE == "RAM" || ROLE == "RARL" || ROLE == "AD" || ROLE == "IE")
        {
            gvStatusDashboard.Columns[10].Visible = false;
            gvStatusDashboard.Columns[12].Visible = false;
            gvStatusDashboard.Columns[13].Visible = false;
            gvStatusDashboard.Columns[7].Visible = false;
            if (ROLE == "RAM" && (SID == 5 || SID == 6 || SID == 24))
            {
                HideColumns(true);
            }
            else if (ROLE == "RARL" && (SID == 5 || SID == 10 || SID == 24))
            {
                HideColumns(true);
            }
            else if (ROLE == "AD" && SID == 12)
            {
                HideColumns(true);
            }
            else if (ROLE == "IE" && SID == 14)
            {
                HideColumns(true);
            }
            else
            {
                HideColumns(false);
            }
            if (SID == 18)
            {
                gvStatusDashboard.Columns[10].Visible = true;
            }
            else
            {
                gvStatusDashboard.Columns[10].Visible = false;
            }
            gvAllocatedAssessor.Columns[11].Visible = false;
            gvAllocatedAssessor.Columns[13].Visible = false;
            gvStatusDashboard.Columns[11].Visible = false;
            gvAssigningAssessor.Columns[11].Visible = false;
            gvAllocatedAssessor.Columns[15].Visible = false;
            gvAllocatedAssessor.Columns[16].Visible = false;
        }
    }
    public void HideColumns(bool FLAG)
    {
        if (FLAG)
        {
            gvStatusDashboard.Columns[8].Visible = false;
            gvStatusDashboard.Columns[9].Visible = true;
        }
        else
        {
            gvStatusDashboard.Columns[8].Visible = true;
            gvStatusDashboard.Columns[9].Visible = false;
        }
    }
    protected void gvAssigningAssessor_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToUpper() == "POPUP")
        {
            string name = e.CommandArgument.ToString();
            if (name != "")
            {
                string[] splitname = name.Split('=');
                if (splitname.Length == 8)
                {
                    Session["CandidateDetails"] = splitname;
                    //Session["CandidateID"] = splitname[0].ToString();
                    //Session["RegnNo"] = splitname[1].ToString();
                    //Session["CandType"] = splitname[2].ToString();
                    //Session["Country"] = splitname[3].ToString();
                }
            }
            mp1.Show();
        }
    }
    protected void PopUpClose_Click(object sender, EventArgs e)
    {
        int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
        lblErrorMessage.Text = "";
        mp1.Hide();
    }

    [WebMethod]
    public void CallClose()
    {
        PopUpClose_Click(null, null);
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("../home/Dashboard.aspx");
    }

    protected void gvStatusDashboard_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var ddl = e.Row.FindControl("ddlRDAccName") as DropDownList;
            if (ddl != null)
            {
                ActivityMasterBAL objActMstBAL = new ActivityMasterBAL();
                DataSet objjActMstDs = new DataSet();

                objjActMstDs = objActMstBAL.GetActivityByActivityType("ACCREDITATIONTYPE");
                CommonUtility.BindDropDown(ref ddl, objjActMstDs, "ActivityName", "ActivityCode");
                ddl.SelectedValue  = e.Row.Cells[15].Text;
                
            }
        }
    }
}